<?php

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class MobilePage extends Page {

    public $footer = array(); //底部
    public $followBar = false; //关注条
    protected $merch_user = array();

    public function __construct() {
        global $_W,$_GPC;

        //检测商城是否关闭
        m('shop')->checkClose();

        if(!empty($_GPC['mid'])){
            isetcookie('mid', $_GPC['mid'],7200);
        }

        $preview = intval($_GPC['preview']);

        $wap = m('common')->getSysset('wap');
        if($wap['open'] && !is_weixin() && empty($preview)) {
            if ($this instanceof MobileLoginPage || $this instanceof PluginMobileLoginPage) {
                if (empty($_W['openid'])) {
                    //$_W['openid'] = m('account')->checkLogin();
                }
            }else{
                //$_W['openid'] = m('account')->checkOpenid();
            }
        }else{

            if ($preview&&!is_weixin())
            {
                $_W['openid'] = "ooyv91cPbLRIz1qaX7Fim_cRfjZk";
            }
            if (MX_SHOP_DEBUG)
            {
                $_W['openid'] = "ooyv91cPbLRIz1qaX7Fim_cRfjZk";
            }
        }

        //获取当前用户
        //$member = m('userinfo')->checkUser();
        $_W['mid'] = !empty($member) ? $member['id'] : '';
        $_W['mopenid'] = !empty($member) ? $member['openid'] : '';
        $merch_data = m('common')->getPluginset('merch');
        if (!empty($_GPC['merchid']) && (!empty(p('merch')) && $merch_data['is_openmerch']))
        {
            $this->merch_user = pdo_fetch("select * from ".tablename('mx_shop_merch_user')." where id=:id limit 1",array(':id'=>intval($_GPC['merchid'])));
        }
    }

    public function shopShare() {
        global $_W,$_GPC;

        $trigger = false;
        if (empty($_W['shopshare'])) {

            $set = $_W['shopset'];
            $_W['shopshare'] = array(
                'title' => empty($set['share']['title']) ? $set['shop']['name'] : $set['share']['title'],
                'imgUrl' => empty($set['share']['icon']) ? tomedia($set['shop']['logo']) : tomedia($set['share']['icon']),
                'desc' => empty($set['share']['desc']) ? $set['shop']['description'] : $set['share']['desc'],
                'link' => empty($set['share']['url']) ? mobileUrl('',null,true) : $set['share']['url']
            );

            if (!empty($_GPC['mid'])) {
                $trigger = true;
                $_W['shopshare']['link'] = mobileUrl('', array('mid' => $_GPC['mid']),true);
            }
        }
        return $trigger;
    }

    public function backliving() {
        global $_W, $_GPC;
        if(!p('live')){
            return;
        }
        if(strexists($_W['routes'], 'live')){
            return false;
        }
        $liveid = intval($_GPC['liveid']);
        if(empty($liveid)){
            return;
        }
        $living = p('live')->isLiving($liveid);
        if(!$living){
            return;
        }
        include $this->template('live/backliving');
    }

    public function wapQrcode(){

        global $_W,$_GPC;
        $currenturl = '';
        if(!is_mobile()){
            $currenturl = "{$_W['siteroot']}app/index.php?{$_SERVER['QUERY_STRING']}";
        }
        $shop = m('common')->getSysset('shop');
        $shopname = $shop['name'];
        include $this->template('_wapqrcode');
    }

    /*
     * 获取令牌
     * @return string $token
     */
    public function getToken()
    {
        global $_W,$_GPC;

        $long = mt_rand(12,24);

        $rand = random($long);

        $token = strtoupper(md5('__cgc_token_'.date('YmdH').'_'.$rand));

        $key = md5($token);
        isetcookie($key, $rand,7200);

        return $token;
    }

    /*
     * 检测令牌
     * @param string $token 需验证的token
     * @return bool
     */
    public function checkToken($token='')
    {
        global $_W,$_GPC;

        if(empty($token))
        {
            return false;
        }

        $key = md5($token);
        if(empty($_GPC[$key]))
        {
            return false;
        }

        $tokent = strtoupper(md5('__cgc_token_'.date('YmdH').'_'.$_GPC[$key]));

        if($token != $tokent)
        {
            return false;
        }

        return true;
    }
}
	
